﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public static class GUILayoutEx
{

    public static bool Title(string title, bool foldout)
    {
        EditorGUILayout.BeginHorizontal(GUITheme.ListItemHeaderStyle, new GUILayoutOption[0]);
        Color backgroundColor = GUI.backgroundColor;
        if (!foldout)
        {
            GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
        }
        foldout = GUILayout.Toggle(foldout, title, EditorStyles.foldout, new GUILayoutOption[0]);
        EditorGUILayout.EndHorizontal();
        GUI.backgroundColor = backgroundColor;
        return foldout;
    }

    public static void BeginContents(GUIStyle style)
    {
        GUILayout.BeginVertical(style, new GUILayoutOption[0]);
    }

    public static void BeginContents()
    {
        GUILayoutEx.BeginContents(GUI.skin.textArea);
    }

    public static void EndContents()
    {
        GUILayout.EndVertical();
    }

    public static bool ActionButton(string label, params GUILayoutOption[] options)
    {
        Color backgroundColor = GUI.backgroundColor;
        GUI.backgroundColor = GUITheme.AcceptColor;
        bool result = false;
        if (GUILayout.Button(label, GUITheme.ActionButtonStyle, options))
        {
            result = true;
        }
        GUI.backgroundColor = backgroundColor;
        return result;
    }


    public static bool AddIconButton()
    {
        return GUILayout.Button("", GUITheme.IconButtonStyle, new GUILayoutOption[0]);
    }

    public static bool DeleteIconButton()
    {
        return GUILayout.Button("", GUITheme.IconButtonStyle, new GUILayoutOption[0]);
    }

    public static bool QuestionIconButton()
    {
        return GUILayout.Button("", GUITheme.IconButtonStyle, new GUILayoutOption[0]);
    }


    public static Rect DragBar()
    {
        Color color = GUI.color;
        GUI.color = Color.white;
        GUILayout.Label("", GUITheme.DragBarStyle, new GUILayoutOption[]
        {
                GUILayout.ExpandHeight(false),
                GUILayout.ExpandWidth(false)
        });
        Rect lastRect = GUILayoutUtility.GetLastRect();
        GUI.color = color;
        return lastRect;
    }

    public static void ProgressBar(float value, string text)
    {
        GUILayoutEx.ProgressBar(value, text, new GUILayoutOption[]
        {
                GUILayout.ExpandWidth(true)
        });
    }

    public static void ProgressBar(float value, string text, params GUILayoutOption[] options)
    {
        Rect rect = GUILayoutUtility.GetRect(GUIContent.none, GUI.skin.box, options);
        EditorGUI.ProgressBar(rect, value, text);
    }

    public static Vector2 ScrollViewFixHeight<T>(Vector2 scrollPosition, IList<T> list, float itemHeight,float height, Action<T, int> draw)
    {
        //当前GUi绘制事件
        if (Event.current.type == EventType.Layout)
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUI.skin.textArea, GUILayout.ExpandWidth(true), GUILayout.Height(height));
        }
        else
        {
            EditorGUILayout.BeginScrollView(scrollPosition, GUI.skin.textArea, new GUILayoutOption[0]);
        }
        int num = (int)(scrollPosition.y / itemHeight);
        num = Mathf.Clamp(num, 0, list.Count);
        //最多显示50条
        int num2 = Mathf.Min(num + 50, list.Count);
        num2 = Mathf.Clamp(num2, 0, list.Count);
        GUILayout.Space((float)num * itemHeight);
        for (int i = num; i < num2; i++)
        {
            draw.Invoke(list[i], i);
        }
        //超过50条的，显示空白区域
        GUILayout.Space((float)(list.Count - num2) * itemHeight);
        EditorGUILayout.EndScrollView();
        return scrollPosition;
    }

}
